<?php

namespace Boxman\Controller;

use Boxman\Controller\BaseController;

class BoxmanController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        if (!$this->boxman_id) {
            $this->error('请先登录');
        }
    }
    
    public function updateBoxmanInfo(){
        $boxman_id = $this->getParam('boxman_id',0,'int+',TRUE);
        $boxman_name = $this->getParam('boxman_name', '', 'string', TRUE);
        $boxman_face = $this->getParam('boxman_face');
        !empty($boxman_name) && $data['boxman_name'] = $boxman_name;
        if(!empty($boxman_face)){
            $data['boxman_face'] = $this->uploadFileBase64($boxman_face);
        }
        $res = M('boxman')->where(['boxman_id'=>$boxman_id])->save($data);
        $this->success();
    }
    
    //消息列表
    public function msglist(){
        $where['planfrom'] = 2;
        $where['user_id'] = $this->boxman_id;
        $res = M('msg')->where($where)->save(['is_read'=>1]);
        $list = M('msg')->where($where)->order('msg_id DESC')->select();
        $this->returnList($list);
    }
    
    //未读消息
    public function msgNoReadcount(){
        $where['planfrom'] = 2;
        $where['user_id'] = $this->boxman_id;
        $where['is_read'] = 0;
        $data['msg_no_read_count'] = M('msg')->where($where)->count();
        $this->returnList($data);
    }
    
    //更新已读
    public function updateMsgRead(){
        $where['msg_id'] = $this->getParam('msg_id',0,'int+',TRUE);
        $where['planfrom'] = 2;
        $res = M('msg')->where($where)->save(['is_read'=>1]);
        $this->success();
    }
    
    public function getBoxmanInfo(){
        $where['boxman_id'] = $this->boxman_id;
        $userInfo = M('boxman')->where($where)->find();
        !empty($userInfo['boxman_face']) && $userInfo['boxman_face'] = C('SITE_URL'). $userInfo['boxman_face'];
        $this->returnList($userInfo);
    }
    
}
